package com.cqq.pro.util;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/*
 * 指挥任务、任务流水枚举类
 */
public class OrderNuMUitl {

	private static Map<Integer,String> numMap = new HashMap<Integer, String>();
	static{
		numMap.put(0, "");
		numMap.put(1, "");
		numMap.put(2, "0");
		numMap.put(3, "00");
		numMap.put(4, "000");
		numMap.put(5, "0000");
	}
	
	
	/**
	 * 
	 * @param oldTaskNum(新的任务编号)
	 * @param dateFormate（任务配置：日期格式）
	 * @param numLength（任务配置：数字长度）
	 * @return
	 */
	
	public static synchronized String getNewTaskNum(String oldTaskNum,String name,String dateFormate,int numLength){
		String newTaskNum = "";//新任务编号
		if(oldTaskNum==null||oldTaskNum.trim().length()<=0){
			String dateStr = DateUtil.formatDate(new Date(),dateFormate);
			String numStr = numMap.get(numLength)+"1";
			newTaskNum = name+dateStr+numStr;
		}else{
			String dateStr = oldTaskNum.substring(0,oldTaskNum.length()-numLength);
			String numStr = oldTaskNum.substring(oldTaskNum.length()-numLength,oldTaskNum.length());
			Integer num = Integer.parseInt(numStr)+1;
			if(num<10){
				numStr = numMap.get(numLength)+num;
			}else if(num<100){
				numStr = numMap.get(numLength-1)+num;
			}else if (num<1000){
				numStr = numMap.get(numLength-2)+num;
			}else if(num<10000){
				numStr = numMap.get(numLength-3)+num;
			}
			newTaskNum = dateStr+numStr;
			if(newTaskNum.length()!=(name.length()+dateFormate.length()+numLength))
				newTaskNum = null;
		}
		return newTaskNum;
	};
	
	public static void main(String[] args) {
		String name ="YZ";
		String dateFormate = "yyyyMMdd";
		int numLength = 3;
		String oldNum = "YZ20150711009";
		System.out.println(getNewTaskNum(oldNum,name,dateFormate,numLength));
	
	}
}
